using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 权限实体
    /// 用于定义系统中的各种操作权限，支持多租户隔离
    /// </summary>
    [Table("permissions")]
    public class Permission : ITenantEntity
    {
        /// <summary>
        /// 权限标识符
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 租户ID
        /// 用于多租户数据隔离
        /// </summary>
        [Required]
        [StringLength(50)]
        public string TenantId { get; set; } = string.Empty;
        
        /// <summary>
        /// 权限名称
        /// 权限的显示名称
        /// </summary>
        [Required]
        [StringLength(100)]
        public string Name { get; set; } = string.Empty;
        
        /// <summary>
        /// 权限代码/标识符
        /// 系统中的唯一标识符，用于代码中引用
        /// </summary>
        [Required]
        [StringLength(100)]
        public string Code { get; set; } = string.Empty;
        
        /// <summary>
        /// 权限描述
        /// 对权限功能的详细说明
        /// </summary>
        [Column(TypeName = "text")]
        public string? Description { get; set; }
        
        /// <summary>
        /// 权限类型
        /// 如：菜单、按钮、API、数据等
        /// </summary>
        [StringLength(50)]
        public string? Type { get; set; }
        
        /// <summary>
        /// 父权限ID（层级结构）
        /// 用于构建权限树
        /// </summary>
        public long? ParentId { get; set; }
        
        /// <summary>
        /// 资源标识
        /// 权限控制的资源或功能模块标识
        /// </summary>
        [StringLength(200)]
        public string? Resource { get; set; }
        
        /// <summary>
        /// 操作动作
        /// 如：create, read, update, delete等
        /// </summary>
        [StringLength(50)]
        public string? Action { get; set; }
        
        /// <summary>
        /// 权限级别
        /// 数值越高权限级别越高
        /// </summary>
        public int Level { get; set; } = 0;
        
        /// <summary>
        /// 图标
        /// 用于菜单显示的图标
        /// </summary>
        [StringLength(100)]
        public string? Icon { get; set; }
        
        /// <summary>
        /// 路由路径
        /// 前端路由路径（对于菜单类型权限）
        /// </summary>
        [StringLength(200)]
        public string? Path { get; set; }
        
        /// <summary>
        /// 是否系统内置权限
        /// </summary>
        public bool IsSystem { get; set; } = false;
        
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsActive { get; set; } = true;
        
        /// <summary>
        /// 排序权重
        /// </summary>
        public int SortOrder { get; set; } = 0;
        
        /// <summary>
        /// 备注信息
        /// </summary>
        [Column(TypeName = "text")]
        public string? Remark { get; set; }
        
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
        
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        // 导航属性
        /// <summary>
        /// 父权限导航属性
        /// </summary>
        [ForeignKey(nameof(ParentId))]
        public Permission? Parent { get; set; }
        
        /// <summary>
        /// 子权限集合
        /// </summary>
        [InverseProperty(nameof(Parent))]
        public ICollection<Permission> Children { get; set; } = new List<Permission>();
        
        /// <summary>
        /// 角色权限关联集合
        /// </summary>
        [InverseProperty(nameof(RolePermission.Permission))]
        public ICollection<RolePermission> RolePermissions { get; set; } = new List<RolePermission>();
    }
}
